﻿Partial Public Class Skin

    Public SkinFiles As New Dictionary(Of String, IO.FileInfo)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim open As New System.Windows.Forms.OpenFileDialog
        open.Filter = "eXtensible Markup Application Files(*.xaml)|*.xaml"

        If open.ShowDialog = Forms.DialogResult.OK Then
            Dim ipfile As New IO.FileInfo(open.FileName)
            Dim ipstorage As New IO.FileInfo(ipfile.FullName & ".storage")
            Dim opfile As New IO.FileInfo(StardustDir & "\Skins\" & ipfile.Name)
            Dim opdir As New IO.DirectoryInfo(StardustDir & "\Skins\" & ipstorage.Name)

            Try
                ipfile.CopyTo(opfile.FullName, True)
                If ipstorage.Exists = True Then
                    ipstorage.CopyTo(opdir.FullName, True)
                End If

                SkinFiles.Add(opfile.Name, opfile)
                ListBox1.Items.Add(opfile.Name)

            Catch ex As Exception
            End Try

        End If
    End Sub

    Private Sub Skin_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim sd As New IO.DirectoryInfo(StardustDir & "\Skins")
        If sd.Exists = False Then
            sd.Create()
        End If
        For Each file As IO.FileInfo In sd.GetFiles("*.xaml")
            SkinFiles.Add(file.Name, file)
            ListBox1.Items.Add(file.Name)
        Next

        Dim ThemeFiles = From item In New IO.DirectoryInfo("Themes").GetFiles Where item.Extension = ".xaml"
                         Select item.FullName, item.Name

        For Each item In ThemeFiles
            SkinFiles.Add(item.Name, New IO.FileInfo(item.FullName))
            ListBox1.Items.Add(item.Name)
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
        Try
            System.Diagnostics.Process.Start(StardustDir & "\Skins")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ListBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ListBox1.SelectionChanged
        Try
            Dim file As IO.FileInfo = SkinFiles.Item(ListBox1.SelectedItem.ToString)
            'ListBox1.SelectedIndex = -1
            'Dim rd As ResourceDictionary = Nothing
            'Using fs As New IO.FileStream(file.FullName, IO.FileMode.Open, IO.FileAccess.Read)
            '    rd = CType(Windows.Markup.XamlReader.Load(fs), ResourceDictionary)
            'End Using

            Dim rd As ResourceDictionary = Core.Render.LoadXaml(Of ResourceDictionary)(file.FullName)

            Application.Current.Resources = rd
            My.Settings.SkinFile = file.FullName
            My.Settings.Save()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        System.Diagnostics.Process.Start("http://www.deepskylabs.com/stardust/skins")
    End Sub

    Private Sub Button_Default_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim rd As ResourceDictionary =
            Stardust.Core.Render.LoadXaml(Of ResourceDictionary)(
                "Themes\Default.xaml.exclude")

        Application.Current.Resources = rd

        My.Settings.SkinFile = ""
        My.Settings.Save()
    End Sub
End Class
